﻿<%@ WebHandler Language="C#" Class="WorkReportDetailInfo" %>

using System;
using System.Web;
using Chenxin.Lib.Utility;
using System.Data;
using System.Text;
using System.Data.SqlClient;

public class WorkReportDetailInfo : AjaxHandlerBase
{

    public WorkReportDetailInfo()
    {
        //编辑工作报告
        htTypePermit.Add("editreply", Constants.Permit_EditWorkReport);
        //删除工作报告
        htTypePermit.Add("delreply", Constants.Permit_DelWorkReport);
    }
    
    override public void RequestProcess(HttpContext context)
    {
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        if (!string.IsNullOrEmpty(context.Request.Params["op"]))
        {
            //处理类型
            string strOperate = context.Request.Params["op"];

            switch (strOperate.Trim())
            {
                case "getreply":
                    GetWordReplyTable(context);
                    break;
                case "addreply":
                    AddReply(context);
                    break;
                case "delreply":
                    DelReply(context);
                    break;
                case "fileupload":
                    Fileupload(context, Constants.FilePath_WorkReport);
                    break;
                default:
                    break;
            }
        }
    }


    public void GetWordReplyTable(HttpContext context)
    {
        try
        {
            if (string.IsNullOrEmpty(context.Request["recordid"]))
                context.Response.Write(retExecResult(Constants.ExecResult.Error, "标识不存在！", ""));

            string strRootId = context.Request["rootid"];
            string strRecordId = context.Request["recordid"];

            StringBuilder sb = new StringBuilder();
            
            int count = 0;
            Constants.Pagination stuPg = new Constants.Pagination();

            //页码
            int strPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            //页显示行数
            int strPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数
            //欲选择字段列表 
            string FieldList = "*";
            //子查询欲选择字段列表
            string TableFieldList = "w.RecordId, w.WorkReportId, w.[Content], w.SendTime, w.UserName, w.UserCode, a.[Path], a.[Type], a.FileName";
            //所要查询的表名
            string strTableName = "WorkReport_Reply as w left join Attachment as a on w.RecordId = a.RelationId ";
            //排序
            string strOrderField = "w.SendTime desc";
            string strWhere = " " + string.Format(" w.WorkReportId = '{0}' and (a.[Type] = 20 or a.[Type] is null) ", new Guid(strRecordId));
            //根据sql执行查询
            DataSet ds = Chenxin.Lib.Hr.DAL.WorkReport_InBox.GetList(strPageSize, strPageIndex, strOrderField, strTableName, strWhere, FieldList, TableFieldList, true);

            count = (int)ds.Tables[1].Rows[0][0];
            
            if (ds != null && ds.Tables[0].Rows.Count != 0)
            {

                sb.Append("<table width=\"100%\" class=\"tabsorternew tab_workreport\" cellspacing=\"1\" >");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string pathname = dr["FileName"].ToString();
                    if (!string.IsNullOrEmpty(pathname) && pathname.Length > 20)
                    {
                        pathname = pathname.Substring(0, 20) + "...";
                    }
                    sb.Append("<tr><td colspan=\"2\"><div style=\"\"></div></td></tr>");
                    sb.Append("<tr>");
                    sb.Append("<td style=\"width:180px; vertical-align:1px;\">");
                    sb.AppendFormat("<div style='padding:22px 0 0 7px;'><span style='line-height:20px;'><span>回复者工号：{0}</span><br>", dr["UserCode"].ToString());
                    sb.AppendFormat("<span>回复者姓名：{0}</span><br>", dr["UserName"].ToString());
                    sb.AppendFormat("<span>回复时间：{0}</span><br>", dr["SendTime"].ToString());
                    if (System.IO.File.Exists(context.Server.MapPath(Constants.DIRECTORY + dr["Path"].ToString())))
                        sb.AppendFormat("<span>附件：<a href=\"/service/Download.ashx?file=/{0}\" title=\"{1}\" id=\"adownload\" target=\"_blank\">{2}</a></span><br>", dr["Path"].ToString(), dr["FileName"].ToString(), pathname);
                    else
                        sb.Append("<span>附件：无附件</span><br>");
                    sb.Append("</span></div><div class=\"btn\"><a href=\"javascript:void(0)\" title=\"复制到剪切板\" name=\"btn_copyreply\"><span>复制回复</span></a></div>");
                    sb.Append("</td>");
                    sb.Append("<td class='tab_report_td'>");
                    string strContent = System.Text.RegularExpressions.Regex.Replace(context.Server.UrlDecode(dr["Content"].ToString()), "<[^>]+>", "<br> &nbsp;&nbsp;&nbsp;");
                    sb.AppendFormat("<div style='padding:10px;'><span style='line-height:normal;'>{0}</span></div>", context.Server.UrlDecode(dr["Content"].ToString()));
                    sb.Append("</td>");
                    sb.Append("</tr>");
                }
                sb.Append("</table>");
            }

            stuPg.count = count;
            stuPg.data = sb.ToString();

            context.Response.Write(retExecResult(Constants.ExecResult.Succ, "OK", stuPg));
            
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    public void AddReply(HttpContext context)
    {
        try
        {
            if (string.IsNullOrEmpty(context.Request["contents"]))
                context.Response.Write(retExecResult(Constants.ExecResult.Error, "回复内容不能为空，请重试！", ""));

            string strSubject = context.Request["subjects"];
            string strContent = context.Request["contents"];
            string strRootId = context.Request["rootid"];
            string strRecordId = context.Request["recordid"];

            //主题信息
            Chenxin.Lib.Hr.Entity.WorkReport_OutBox workreport_outbox_main = Chenxin.Lib.Hr.BLL.WorkReport_OutBox.GetModel(new Guid(strRootId));
            Chenxin.Lib.Hr.Entity.WorkReport_OutBox workreport_outbox_then = null;
            if (!strRootId.Equals(strRecordId))
            {
                //当前回复主题信息
                workreport_outbox_then = Chenxin.Lib.Hr.BLL.WorkReport_OutBox.GetModel(new Guid(strRecordId));
            }
            //回复表信息
            Chenxin.Lib.Hr.Entity.WorkReport_Reply model = new Chenxin.Lib.Hr.Entity.WorkReport_Reply();
            model.RecordId = Guid.NewGuid();
            model.WorkReportId = new Guid(strRecordId);
            model.Subject = strSubject;
            model.Content = strContent;
            model.SendTime = DateTime.Now;
            model.UserId = CurrentUserInfo.UserId;
            model.UserName = CurrentUserInfo.UserName;
            model.UserCode = CurrentUserInfo.UserCode;
            model.DepartId = CurrentUserInfo.DepartId;
            model.DepartName = CurrentUserInfo.DepartName;
            model.CompanyId = CurrentUserInfo.CompanyId;
            model.CompanyName = CurrentUserInfo.CompanyName;
            model.CityId = CurrentUserInfo.CityId;
            model.CityName = CurrentUserInfo.CityName;
            Chenxin.Lib.Hr.BLL.WorkReport_Reply.Add(model);

            //回复表附件信息
            if (!string.IsNullOrEmpty(context.Request["filename"]))
            {
                AddAttachments(model.RecordId, Constants.FilePath_WorkReport + context.Request["savename"].ToString(), context.Request["filename"].ToString(), 20);
            }

            
            //发件箱信息
            Chenxin.Lib.Hr.Entity.WorkReport_OutBox workreport_outbox = new Chenxin.Lib.Hr.Entity.WorkReport_OutBox();
            workreport_outbox.RecordId = model.RecordId;
            workreport_outbox.SenderId = CurrentUserInfo.UserId;
            workreport_outbox.SenderName = CurrentUserInfo.UserName;
            workreport_outbox.SenderCityId = CurrentUserInfo.CityId;
            workreport_outbox.SenderCityName = CurrentUserInfo.CityName;
            workreport_outbox.SenderCode = CurrentUserInfo.UserCode;
            workreport_outbox.SenderCompanyId = CurrentUserInfo.CompanyId;
            workreport_outbox.SenderCompanyName = CurrentUserInfo.CompanyName;
            workreport_outbox.SenderDepartId = CurrentUserInfo.DepartId;
            workreport_outbox.SenderDepartName = CurrentUserInfo.DepartName;
            workreport_outbox.SendTime = DateTime.Now;
            if (!strRootId.Equals(strRecordId))
            {
                workreport_outbox.ReceiverId = workreport_outbox_then.SenderId.ToString();
                workreport_outbox.ReceiverName = workreport_outbox_then.SenderName;
            }
            else
            {
                workreport_outbox.ReceiverId = workreport_outbox_main.SenderId.ToString();
                workreport_outbox.ReceiverName = workreport_outbox_main.SenderName;
            }
            workreport_outbox.Subject = strSubject;
            workreport_outbox.Content = strContent;
            workreport_outbox.isDeleted = false;
            //判断其是(主题)还是(回复)
            if (workreport_outbox_main.ParentId.Equals(Guid.Empty) && workreport_outbox_main.RootId.Equals(Guid.Empty))
            {
                workreport_outbox.ParentId = new Guid(context.Request["recordid"]);
                workreport_outbox.RootId = workreport_outbox_main.RecordId;
            }
            Chenxin.Lib.Hr.BLL.WorkReport_OutBox.Add(workreport_outbox);

            //回复表附件信息
            if (!string.IsNullOrEmpty(context.Request["filename"]))
            {
                AddAttachments(model.RecordId, Constants.FilePath_WorkReport + context.Request["savename"].ToString(), context.Request["filename"].ToString(), 10);
            }
            
            
            //收件箱信息
            Chenxin.Lib.Hr.Entity.WorkReport_InBox workreport_inbox = new Chenxin.Lib.Hr.Entity.WorkReport_InBox();
            workreport_inbox.RecordId = Guid.NewGuid();
            workreport_inbox.WorkReportId = workreport_outbox.RecordId;
            if (!strRootId.Equals(strRecordId))
            {
                workreport_inbox.ReceiverCityId = workreport_outbox_then.SenderCityId;
                workreport_inbox.ReceiverCityName = workreport_outbox_then.SenderCityName;
                workreport_inbox.ReceiverCompanyId = workreport_outbox_then.SenderCompanyId;
                workreport_inbox.ReceiverCompanyName = workreport_outbox_then.SenderCompanyName;
                workreport_inbox.ReceiverDepartId = workreport_outbox_then.SenderDepartId;
                workreport_inbox.ReceiverDepartName = workreport_outbox_then.SenderDepartName;
                workreport_inbox.ReceiverId = workreport_outbox_then.SenderId;
                workreport_inbox.ReceiverName = workreport_outbox_then.SenderName;
            }
            else
            {
                workreport_inbox.ReceiverCityId = workreport_outbox_main.SenderCityId;
                workreport_inbox.ReceiverCityName = workreport_outbox_main.SenderCityName;
                workreport_inbox.ReceiverCompanyId = workreport_outbox_main.SenderCompanyId;
                workreport_inbox.ReceiverCompanyName = workreport_outbox_main.SenderCompanyName;
                workreport_inbox.ReceiverDepartId = workreport_outbox_main.SenderDepartId;
                workreport_inbox.ReceiverDepartName = workreport_outbox_main.SenderDepartName;
                workreport_inbox.ReceiverId = workreport_outbox_main.SenderId;
                workreport_inbox.ReceiverName = workreport_outbox_main.SenderName;
            }
            workreport_inbox.isDeleted = false;
            workreport_inbox.isReaded = false;
            Chenxin.Lib.Hr.BLL.WorkReport_InBox.Add(workreport_inbox);
            
            context.Response.Write(retExecResult(Constants.ExecResult.Succ, "回复成功！", ""));
        
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    public void DelReply(HttpContext context)
    {
        try
        {
            if (string.IsNullOrEmpty(context.Request["recordid"]))
                context.Response.Write(retExecResult(Constants.ExecResult.Error, "标识丢失，请重试！", ""));

            Chenxin.Lib.Hr.Entity.WorkReport_OutBox model = Chenxin.Lib.Hr.BLL.WorkReport_OutBox.GetModel(new Guid(context.Request["recordid"]));
            model.isDeleted = true;
            //删除发件箱（逻辑删除）
            Chenxin.Lib.Hr.BLL.WorkReport_OutBox.Update(model);
            //删除收件箱（物理删除）
            Chenxin.Lib.Hr.BLL.WorkReport_InBox.DeleteByReportId(model.RecordId);
            //删除回复信息（物理删除）
            Chenxin.Lib.Hr.BLL.WorkReport_Reply.DeleteByReportId(model.RecordId);

            //删除回复信息（物理删除）
            Chenxin.Lib.Hr.BLL.WorkReport_Reply.Delete(model.RecordId);
            
            context.Response.Write(retExecResult(Constants.ExecResult.Succ, "删除成功！", ""));
            
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

}